<?php

namespace ticky\helper;

class file {

    public static $map = [];

    // 获取文件基本信息
    public static function getInfo($file, $field = '') {
        runlog('error', 'ticky\helper\file\getInfo:' . $file);
        $info = [];
        if (!self::exists($file)) {
            return false;
        }
        $handle = new \SplFileInfo($file);
        $info['size'] = $handle->getSize();
        $info['type'] = $handle->getType();
        $info['extension'] = strtolower($handle->getExtension());
        $info['base_path'] = $handle->getPath();
        $info['file_path'] = $handle->getRealPath();
        $info['base_name'] = $handle->getBasename('.' . $handle->getExtension());
        $info['file_name'] = $handle->getFilename();
        $info['group'] = $handle->getGroup();
        $info['owner'] = $handle->getOwner();
        $info['auth'] = $handle->getPerms();
        $info['last_access_time'] = date('Y-m-d H:i:s', $handle->getATime());
        $info['last_modify_time'] = date('Y-m-d H:i:s', $handle->getMTime());
        $info['isExecutable'] = $handle->isExecutable();
        $info['isReadable'] = $handle->isReadable();
        $info['isWritable'] = $handle->isWritable();
        return $field ? $info[$field] : $info;
    }

    public static function getSize($file) {
        return self::getInfo($file, 'extension');
    }

    // 判断文件是否存在
    public static function exists($file) {
        return file_exists($file);
    }

    // 加载文件
    public static function load($file, $data = null) {
        if (empty($file) || !isset($file)) {
            return false;
        }
        $key = base64_encode($file);
        if (!self::exists($file)) {
            return false;
        }
        if (!is_null($data)) {
            extract($data, EXTR_OVERWRITE);
        }
        if (!empty($key) && !empty(self::$map) && !self::$map[$key]) {
            self::$map[$key] = $file;
            include $file;
        }
        return true;
    }

    // 创建文件
    public static function create($file, $data = null) {
        $str = '';
        if (is_array($data)) {
            foreach ($data as $k => $v) {
                $str .= $k . " : " . $v . "  ";
            }
        } else {
            $str = $data . "\r\n";
        }
        $path = dirname($file);
        dir::exists($path) or dir::create($path);
        if (file_put_contents($file, $str, FILE_APPEND)) {
            return true;
        }
        return false;
    }

    // 删除文件
    public static function delete($file) {
        return self::exists($file) ? unlink($file) : false;
    }

    // 移动文件
    public static function move($oldFile, $newFile) {
        return self::exists($oldFile) ? rename($oldFile, $newFile) : false;
    }

    // 复制文件
    public static function copy($oldFile, $newFile) {
        return self::exists($oldFile) ? copy($oldFile, $newFile) : false;
    }

    public static function writelog($file, $log) {
        $yearmonth = date('Ym');
        $logdir = RUNTIME_PATH . 'log' . DS;
        dir::exists($logdir) or dir::create($logdir);
        $logfile = $logdir . $yearmonth . '_' . $file . '.php';
        if (@filesize($logfile) > 2048000) {
            $dir = opendir($logdir);
            $length = strlen($file);
            $maxid = $id = 0;
            while ($entry = readdir($dir)) {
                if (strpos($entry, $yearmonth . '_' . $file) !== false) {
                    $id = intval(substr($entry, $length + 8, -4));
                    $id > $maxid && $maxid = $id;
                }
            }
            closedir($dir);
            $logfilebak = $logdir . $yearmonth . '_' . $file . '_' . ($maxid + 1) . '.php';
            @rename($logfile, $logfilebak);
        }
        if ($fp = @fopen($logfile, 'a')) {
            @flock($fp, 2);
            if (!is_array($log)) {
                $log = array($log);
            }
            foreach ($log as $tmp) {
                fwrite($fp, "<?PHP exit;?>" . str_replace(array('<?', '?>'), '', $tmp));
            }
            fclose($fp);
        }
    }

}
